package com.leetcode.algorithm.y22.m09.w2;

import java.util.Arrays;

/**
 * 1710. 卡车上的最大单元数
 * 
 * https://leetcode.cn/problems/maximum-units-on-a-truck/
 * 
 * @author jie.deng
 *
 */
class Question1710Solution01 {
	
	public int maximumUnits(int[][] boxTypes, int truckSize) {
		Arrays.sort(boxTypes, (arr1, arr2) -> arr2[1] - arr1[1]);
		int numberOfUnits = 0;
		for (int i = 0; i < boxTypes.length; i++) {
			int[] arr = boxTypes[i];
			if (arr[0] < truckSize) {
				truckSize -= arr[0];
				numberOfUnits += arr[0] * arr[1];
			} else {
				numberOfUnits += truckSize * arr[1];
				break;
			}
		}
		return numberOfUnits;
	}
    
}